package com.nthinkers.sabq.activities;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;

import com.nthinkers.sabq.R;

public class HomeActivity extends BaseActivity {
	private static final String TAG = "HomeActivity";

	Button btnLocal, btnSports, btnTopics, btnMore, btnAboutUs;
	WebView wbvContent, wbvAds;
	ProgressBar prbLoading;
	Dialog mMoreDialog;

	private static final String URL_LOCAL_NEWS = "http://www.sabq.sa/iphone/arabicnews/5";
	private static final String URL_SPORTS = "http://www.sabq.sa/iphone/arabicnews/2";
	private static final String URL_TOPICS = "http://www.sabq.sa/iphone/arabicnews/4";
	private static final String URL_MORE = "http://www.sabq.sa/iphone/tabs2";
	private static final String URL_HANDSET_ADS = "http://www.sabq.sa/iphone/advs";
	private static final String URL_TABLET_ADS = "http://www.sabq.sa/ipad/advs";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.screen_home);
		setupWidgets();

		wbvContent.loadUrl(URL_LOCAL_NEWS);

		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);

		int width = metrics.widthPixels;

		if (width >= 600)
			wbvAds.loadUrl(URL_TABLET_ADS);
		else
			wbvAds.loadUrl(URL_HANDSET_ADS);

	}

	private void setupWidgets() {
		wbvContent = (WebView) findViewById(R.id.home_wbv_content);
		wbvContent.getSettings().setJavaScriptEnabled(true);
		wbvContent.setWebViewClient(new SabqWebViewClient());

		wbvAds = (WebView) findViewById(R.id.home_wbv_ads);
		wbvAds.getSettings().setJavaScriptEnabled(true);
		wbvAds.setWebViewClient(new SabqWebViewClient());

		prbLoading = (ProgressBar) findViewById(R.id.home_prb_loading);

		btnLocal = (Button) findViewById(R.id.home_btn_local);
		btnSports = (Button) findViewById(R.id.home_btn_sports);
		btnTopics = (Button) findViewById(R.id.home_btn_topics);
		btnMore = (Button) findViewById(R.id.home_btn_more);
		btnAboutUs = (Button) findViewById(R.id.home_btn_about_us);

		btnLocal.setDrawingCacheEnabled(false);
		btnSports.setDrawingCacheEnabled(false);
		btnTopics.setDrawingCacheEnabled(false);
		btnMore.setDrawingCacheEnabled(false);
		btnAboutUs.setDrawingCacheEnabled(false);

		btnLocal.setTag(true);
		btnLocal.setBackgroundResource(R.drawable.selector_menu_button_inverse);

		btnLocal.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				boolean isClicked = false;
				if (btnLocal.getTag() != null)
					isClicked = (Boolean) btnLocal.getTag();
				if (!isClicked) {
					wbvContent.loadUrl(URL_LOCAL_NEWS);
					btnLocal.setTag(true);
					btnLocal.setBackgroundResource(R.drawable.selector_menu_button_inverse);
					uncheckOtherTabs(btnLocal);
				}

			}
		});

		btnSports.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				boolean isClicked = false;
				if (btnSports.getTag() != null)
					isClicked = (Boolean) btnSports.getTag();
				if (!isClicked) {
					wbvContent.loadUrl(URL_SPORTS);
					btnSports.setTag(true);
					btnSports
							.setBackgroundResource(R.drawable.selector_menu_button_inverse);
					uncheckOtherTabs(btnSports);
				}

			}
		});

		btnTopics.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				boolean isClicked = false;
				if (btnTopics.getTag() != null)
					isClicked = (Boolean) btnTopics.getTag();
				if (!isClicked) {
					wbvContent.loadUrl(URL_TOPICS);
					btnTopics.setTag(true);
					btnTopics
							.setBackgroundResource(R.drawable.selector_menu_button_inverse);
					uncheckOtherTabs(btnTopics);
				}

			}
		});

		btnMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showMoreDialog();
				btnMore.setTag(true);
				btnMore.setBackgroundResource(R.drawable.selector_menu_button_inverse);
				uncheckOtherTabs(btnMore);

			}
		});

		btnAboutUs.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				boolean isClicked = false;
				if (btnAboutUs.getTag() != null)
					isClicked = (Boolean) btnAboutUs.getTag();
				if (!isClicked) {
					Intent intent = new Intent(HomeActivity.this,
							AboutusActivity.class);
					startActivity(intent);
					// btnAboutUs.setTag(true);
					// btnAboutUs
					// .setBackgroundResource(R.drawable.selector_menu_button_inverse);
					// uncheckOtherTabs(btnAboutUs);
				}

			}
		});

	}

	private void uncheckOtherTabs(Button selectedTab) {
		if (selectedTab != btnLocal) {
			btnLocal.setTag(false);
			btnLocal.setBackgroundResource(R.drawable.selector_menu_button);
		}

		if (selectedTab != btnSports) {
			btnSports.setTag(false);
			btnSports.setBackgroundResource(R.drawable.selector_menu_button);
		}

		if (selectedTab != btnTopics) {
			btnTopics.setTag(false);
			btnTopics.setBackgroundResource(R.drawable.selector_menu_button);
		}

		if (selectedTab != btnMore) {
			btnMore.setTag(false);
			btnMore.setBackgroundResource(R.drawable.selector_menu_button);
		}

		if (selectedTab != btnAboutUs) {
			btnAboutUs.setTag(false);
			btnAboutUs.setBackgroundResource(R.drawable.selector_menu_button);
		}

	}

	private void showMoreDialog() {
		mMoreDialog = new Dialog(this,
				android.R.style.Theme_Translucent_NoTitleBar);

		// dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		mMoreDialog.setContentView(R.layout.dialog_more);

		WebView wbvMore = (WebView) mMoreDialog
				.findViewById(R.id.more_wbv_more_topics);
		wbvMore.getSettings().setJavaScriptEnabled(true);
		wbvMore.setWebViewClient(new SabqWebViewClient());
		wbvMore.loadUrl(URL_MORE);
		mMoreDialog.show();
	}

	private void showLoading() {
		if (mMoreDialog != null && mMoreDialog.isShowing()) {
			ProgressBar loading = (ProgressBar) mMoreDialog
					.findViewById(R.id.more_prb_loading);
			loading.setVisibility(View.VISIBLE);
		} else {
			prbLoading.setVisibility(View.VISIBLE);
		}
	}

	private void hideLoading() {
		if (mMoreDialog != null && mMoreDialog.isShowing()) {
			ProgressBar loading = (ProgressBar) mMoreDialog
					.findViewById(R.id.more_prb_loading);
			loading.setVisibility(View.GONE);
		} else {
			prbLoading.setVisibility(View.GONE);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && wbvContent.canGoBack()) {
			wbvContent.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void startBrowser(String url) {
		Uri uriUrl = Uri.parse(url);
		Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
		startActivity(launchBrowser);

	}

	private class SabqWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			/**
			 * sabq/sabqs, esabq/esabqs should start android browser [12:16:39
			 * PM EET] thaer.mohazia: isab/isabqs are internal in the same
			 * webview [12:17:08 PM EET] Mahdi Hijazi: okay [12:17:10 PM EET]
			 * thaer.mohazia: sabq/sabqs legacy mode :P esabq = external isabq =
			 * internal
			 */

			if (url.startsWith("sabq")) {
				// External link
				url = "http" + url.substring(4);
				startBrowser(url);
			} else if (url.startsWith("sabqs")) {
				// External link
				url = "https" + url.substring(5);
				startBrowser(url);
			} else if (url.startsWith("esabq")) {
				// External link
				url = "http" + url.substring(5);
				startBrowser(url);
			} else if (url.startsWith("esabqs")) {
				// External link
				url = "https" + url.substring(6);
				startBrowser(url);
			} else if (url.startsWith("isabq")) {
				// Internal link
				url = "http" + url.substring(5);
				Log.i(TAG, "url: " + url);
				wbvContent.loadUrl(url);
				mMoreDialog.dismiss();
			} else if (url.startsWith("isabqs")) {
				// Internal link
				url = "https" + url.substring(6);
				Log.i(TAG, "url: " + url);
				wbvContent.loadUrl(url);
				mMoreDialog.dismiss();
			} else {
				view.loadUrl(url);

			}

			return true;
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			hideLoading();
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			// TODO Auto-generated method stub
			super.onPageStarted(view, url, favicon);
			showLoading();

		}
	}

}
